﻿#region Copyright (c) 2011 R. Uittenbosch
/*
* Copyright (C) 2011 R. Uittenbosch
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and 
* associated documentation files (the "Software"), to deal in the Software without restriction, including 
* without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
* copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the 
* following conditions:
* 
* The above copyright notice and this permission notice shall be included in all copies or substantial 
* portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT 
* LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO 
* EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 
* USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;
using System.Collections;
using DatabaseViewer.Interfaces;


namespace DatabaseViewer.Domain
{
    /// <summary>
    /// A node to be visualized
    /// </summary>
    public class VisualizerNode : IVisualizerNode
    {
      
        /// <summary>
        /// Constructor
        /// </summary>
        protected VisualizerNode()
        {            
        }

        /// <summary>
        /// Constructor (defaults to ControlType Tab)
        /// </summary>
        /// <param name="objectType"></param>        
        public VisualizerNode(Type objectType) : this(objectType, ControlType.Tab)
        {          
        }

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="objectType"></param>
        /// <param name="control"></param>
        public VisualizerNode(Type objectType, ControlType control)
        {                  
            Control = control;
            UpdateNode(objectType);  
        }

        public IVisualizerNode AddChildren(IVisualizerNode[] children)
        {
            Children = Children.Concat(children).ToList();
            return this;
        }
     
        /// <summary>
        /// Update the name of the NodeType with the fullname of the calling assembly
        /// </summary>
        protected void UpdateNode(Type type)
        {
            DisplayName = type.Name;
            ObjectType  = type.AssemblyQualifiedName;           
            Children    = new List<IVisualizerNode>();         
        }      
            
        /// <summary>
        /// Is it a leaf or are the children?
        /// </summary>
        public bool    IsLeaf      { get { return Children == null || Children.Count == 0; } }

        /// <summary>
        /// Should we iterate through all underlying objects or just take the first
        /// </summary>
        public bool    Multiple    { get; set; }

        /// <summary>
        /// The name shown on the different tabs.
        /// </summary>
        public string  DisplayName { get; set; }

        /// <summary>
        /// The complete (AssemblyQualifiedName) of the type.
        /// </summary>
        public string  ObjectType  { get; set; }     

        /// <summary>
        /// The controltype on which to visualize the objectdata
        /// </summary>
        public ControlType Control { get; set; }

        /// <summary>
        /// Possible child objects
        /// </summary>
        public IList<IVisualizerNode> Children { get; set; }

        /// <summary>
        /// The datasource to display, this could be a specific one, set by the IDBModel.
        /// </summary>
        public IList DataSource { get; set; }

        /// <summary>
        /// Find a specfic childnode
        /// </summary>
        /// <returns>The node in case it was found, null otherwise.</returns>
        public IVisualizerNode FindNode<T>()
        {
            // If the name qualifies, it's the current object
           if (ObjectType == typeof(T).AssemblyQualifiedName)
                return this;
            else
            {
                // Shortcut for no children
                if (!IsLeaf)
                {
                    foreach (IVisualizerNode n in Children)
                    {
                        // If found, return it, otherwise keep searching
                        IVisualizerNode nf = n.FindNode<T>() ;
                        if (nf != null)
                            return nf;
                    }
                }
            }
            return null;
        }

        /// <summary>
        /// Find a specfic childnode
        /// </summary>
        /// <param name="displayName">The displayname of the node. Normaly the class name.</param>
        /// <returns>The node in case it was found, null otherwise.</returns>
        public IVisualizerNode FindNode(string displayName)
        {
            // If the name qualifies, it's the current object
            if (DisplayName == displayName)
                return this;
            else
            {
                // Shortcut for no children
                if (!IsLeaf)
                {
                    foreach (IVisualizerNode n in Children)
                    {
                        // If found, return it, otherwise keep searching
                        IVisualizerNode nf = n.FindNode(displayName);
                        if (nf != null)
                            return nf;
                    }
                }
            }
            return null;
        }
    }
}
